﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using POSPlus.PrintManager;
using POSPlus.MvcPassiveView;
using POSPlus.EZBarTender;
using POSPlus.EZBarTender.SignsPrinting;
using EZBarTender_Wrapper.EDLP;
using EZBarTender_Wrapper.SmallLabels;
using Catalina.MVP;
using Catalina.MVP;
using Catalina.MVP;
using EZBarTender_Wrapper.Settings;
using Util;

namespace EZBarTender_Wrapper
{
	public partial class EZBarTenderWrapper : Form
	{
		private String _connectionString;
		private string _corpConnectionString;
		private string _regularFileLocation;
		private string _regularSmallFileLocation;
		private string _specialSmallFileLocation;
		private string _specialFileLocation;
		private string _smartBuyFileLocation;
		private string _smartBuySmallFileLocation;
		private string _discontinuedFileLocation;
		private string _discontinuedSmallFileLocation;
		private string _nowEvenLowerFileLocation;
		private string _1upXSignFileLocation;
		private string _1upLSignFileLocation;
		private string _1upMSignFileLocation;
		private string _1upSSignFileLocation;
		private string _2upSignFileLocation;
		private string _4upSignFileLocation;
		private string _SB1upXSignFileLocation;
		private string _SB1upLSignFileLocation;
		private string _SB1upMSignFileLocation;
		private string _SB1upSSignFileLocation;
		private string _SB2upSignFileLocation;
		private string _SB4upSignFileLocation;
		private string _xmlFileLocation;
		private string _catalinaFileLocation;
        private string _discoSmallFileLocation;

		private PrintManagerView _printManagerView;
		private PrintManagerControl _printManagerControl;
		private PrintManagerModel _printManagerModel;
		private BatchPrinter _batchPrinter;

		private SignsEditor _signsEditor;
		private SignsEditorModel _signsEditorModel;
		private SignsEditorControl _signsEditorControl;

		private EdlpPrinter _edlpPrinter;

        private ApplicationConfig appConfig;

		public EZBarTenderWrapper(
            ApplicationConfig appConfig,
			string regularFileLocation, 
			string regularSmallFileLocation,
			string specialSmallFileLocation,
			string specialFileLocation,
			string smartBuyFileLocation,
			string smartBuySmallFileLocation,
			string discontinuedFileLocation,
			string discontinuedSmallFileLocation,
			string nowEvenLowerFileLocation,
			string oneUpXSignFileLocation,
			string oneUpLSignFileLocation,
			string oneUpMSignFileLocation,
			string oneUpSSignFileLocation,
			string twoUpSignFileLocation,
			string fourUpSignFileLocation,
			string SBoneUpXSignFileLocation,
			string SBoneUpLSignFileLocation,
			string SBoneUpMSignFileLocation,
			string SBoneUpSSignFileLocation,
			string SBtwoUpSignFileLocation,
			string SBfourUpSignFileLocation,
			string catalinaFileLocation,
            string discoSmallFileLocation)
		{
			InitializeComponent();
			Location = new Point(5, 5);
            this.appConfig = appConfig;
			_connectionString = appConfig.LocalConnectionString;
			_corpConnectionString = appConfig.CorpConnectionString;
			_regularFileLocation = regularFileLocation;
			_regularSmallFileLocation = regularSmallFileLocation;
			_specialSmallFileLocation = specialSmallFileLocation;
			_specialFileLocation = specialFileLocation;
			_smartBuyFileLocation = smartBuyFileLocation;
			_smartBuySmallFileLocation = smartBuySmallFileLocation;
			_discontinuedFileLocation = discontinuedFileLocation;
			_discontinuedSmallFileLocation = discontinuedSmallFileLocation;
			_nowEvenLowerFileLocation = nowEvenLowerFileLocation;
			_1upXSignFileLocation = oneUpXSignFileLocation;
			_1upLSignFileLocation = oneUpLSignFileLocation;
			_1upMSignFileLocation = oneUpMSignFileLocation;
			_1upSSignFileLocation = oneUpSSignFileLocation;
			_2upSignFileLocation = twoUpSignFileLocation;
			_4upSignFileLocation = fourUpSignFileLocation;
			_SB1upXSignFileLocation = SBoneUpXSignFileLocation;
			_SB1upLSignFileLocation = SBoneUpLSignFileLocation;
			_SB1upMSignFileLocation = SBoneUpMSignFileLocation;
			_SB1upSSignFileLocation = SBoneUpSSignFileLocation;
			_SB2upSignFileLocation = SBtwoUpSignFileLocation;
			_SB4upSignFileLocation = SBfourUpSignFileLocation;
			_catalinaFileLocation = catalinaFileLocation;
            _discoSmallFileLocation = discoSmallFileLocation;
			_xmlFileLocation = appConfig.PrintXml;
		}

		private void ui_printManagerButton_Click(object sender, EventArgs e)
		{
			if (_printManagerControl == null)
			{
				_printManagerView = new PrintManagerView(Location.X, Location.Y + Height + 3);
				_printManagerModel = new PrintManagerModel(
					appConfig, 
					_regularFileLocation,
 					_regularSmallFileLocation,
					_specialSmallFileLocation,
					_specialFileLocation,
 					_smartBuyFileLocation,
					_smartBuySmallFileLocation,
					_discontinuedFileLocation,
					_discontinuedSmallFileLocation,
					_nowEvenLowerFileLocation,
					_1upXSignFileLocation,
					_1upLSignFileLocation,
					_1upMSignFileLocation,
					_1upSSignFileLocation,
					_2upSignFileLocation,
					_4upSignFileLocation,
					_SB1upXSignFileLocation,
					_SB1upLSignFileLocation,
					_SB1upMSignFileLocation,
					_SB1upSSignFileLocation,
					_SB2upSignFileLocation,
					_SB4upSignFileLocation,
                    _discoSmallFileLocation);
				_printManagerControl = 
					new PrintManagerControl(_printManagerModel, _printManagerView);

				_printManagerView.Show();
			}
			else if (_printManagerView.IsDisposed)
			{
				_printManagerView = new PrintManagerView(Location.X, Location.Y + Height + 3);
				_printManagerModel = new PrintManagerModel(
                    appConfig,
					_regularFileLocation,
					_regularSmallFileLocation,
					_specialSmallFileLocation,
					_specialFileLocation, 
					_smartBuyFileLocation,
					_smartBuySmallFileLocation,
					_discontinuedFileLocation,
					_discontinuedSmallFileLocation,
					_nowEvenLowerFileLocation,
					_1upXSignFileLocation,
					_1upLSignFileLocation,
					_1upMSignFileLocation,
					_1upSSignFileLocation,
					_2upSignFileLocation,
					_4upSignFileLocation,
					_SB1upXSignFileLocation,
					_SB1upLSignFileLocation,
					_SB1upMSignFileLocation,
					_SB1upSSignFileLocation,
					_SB2upSignFileLocation,
					_SB4upSignFileLocation,
                    _discoSmallFileLocation);
				_printManagerControl =
					new PrintManagerControl(_printManagerModel, _printManagerView);

				_printManagerView.Show();
			}
			else if (_printManagerView.WindowState == FormWindowState.Minimized)
			{
				_printManagerView.WindowState = FormWindowState.Normal;
			}			
		}

		private void ui_displaySignsPrintingButton_Click(object sender, EventArgs e)
		{
			if (_signsEditorControl == null || _signsEditor.IsDisposed)
			{
				_signsEditor = new SignsEditor(_corpConnectionString, _connectionString, appConfig);
				_signsEditorModel = new SignsEditorModel(_corpConnectionString, _connectionString);
				_signsEditorModel.NotifyEventAction += new SignsEditorModel.NotifyPrintManager(_signsEditor_JobAdded);
				_signsEditorControl = new SignsEditorControl(_signsEditorModel, _signsEditor);

				_signsEditor.Show();
			}
			else if (_signsEditor.WindowState == FormWindowState.Minimized)
			{
				_signsEditor.WindowState = FormWindowState.Normal;
			}
		}

		void _signsEditor_JobAdded()
		{
			if (_printManagerModel != null)
				_printManagerModel.UpdateSignList();
		}

		private void ui_labelPrintingButton_Click(object sender, EventArgs e)
		{
			if (_batchPrinter == null || _batchPrinter.IsDisposed)
			{
				_batchPrinter = new BatchPrinter(appConfig, Width + 6, Location.Y);
				_batchPrinter.OnJobAdded += new BatchPrinter.JobAdded(_batchPrinter_OnJobAdded);
				_batchPrinter.Show();
			}
			else
			{
				_batchPrinter.Show();
			}
		}

		void _batchPrinter_OnJobAdded(object sender, EventArgs e)
		{
			if(_printManagerModel != null)
				_printManagerModel.UpdateSignList();
		}

		private void ui_edlpPrinting_Click(object sender, EventArgs e)
		{
			if (_edlpPrinter == null || _edlpPrinter.IsDisposed)
			{
				_edlpPrinter = new EdlpPrinter(_connectionString);
				_edlpPrinter.OnJobAdded += new EdlpPrinter.JobAdded(_edlpPrinter_OnJobAdded);
				_edlpPrinter.Show();
			}
			else
			{
				_edlpPrinter.Show();
			}
		}

		void _edlpPrinter_OnJobAdded()
		{
			if (_printManagerModel != null)
				_printManagerModel.UpdateSignList();
		}

		private void buttonSmallLabelMaintenance_Click(object sender, EventArgs e)
		{
			SmallLabelSelection selection = new SmallLabelSelection(_connectionString);
			selection.ShowDialog();
		}

		private void catalinaButton_Click(object sender, EventArgs e)
		{
			CatalinaPrintView view = new CatalinaPrintView();
			CatalinaModel model = new CatalinaModel();
			CatalinaPresenter presenter = new CatalinaPresenter(_corpConnectionString, _connectionString, _xmlFileLocation, _catalinaFileLocation, model, view);
			presenter.Initialize();
			view.ShowDialog();
		}

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData.Equals(Keys.F11))
            {
                Settings.SettingsConfig config = new Settings.SettingsConfig();
                config.ShowDialog();
            }
            else if (keyData.Equals(Keys.F7))
            {
                Settings.AboutBox1 box = new Settings.AboutBox1();
                box.ShowDialog();
            }
            
            
            return base.ProcessCmdKey(ref msg, keyData);
        }
	}
}